﻿package  {
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	public class Elevator extends MovieClip{
		private var id;
		private var canvasInner:CanvasInner;
		private var state:String;// up1, down2, close3, open4, stop
		private var destination:Array=new Array();
		private var destinationBuffer:Array=new Array();
		private var found:Found;
		private var canvasOuter:CanvasOuter;
		//private var dynText=stage.getChildByName("here") as TextField;
		
		
		private var direction:String;
		
		
		//timer
		private var timer:Timer;
		
		//data
		private var minY=120;
		private var maxY=500;
		
		public function Elevator(found:Found,canvasOuter:CanvasOuter,pContainer:MovieClip,x:int,y:int,id:int) { 
			reSet(x,y);
			this.id=id;
			//init canvasInner
			canvasInner=new CanvasInner(this,pContainer,x-60,y);
			this.canvasOuter=canvasOuter;
			//listener
			//this.addEventListener("task",eventTask);
			//timer
			timer=new Timer(24,0);
			timer.addEventListener(TimerEvent.TIMER,timeIng);
			timer.start();
			this.found=found;
			pContainer.addChild(this);
		}
		
		public function reSet(x:int,y:int):void{
			this.x=x;
			this.y=y;
			state="stop";
			direction="stop";
		}
		public function getID():int
		{
			return id;
		}
		
		public function getDirection():String
		{
			return direction;
		}
		
		private function timeIng(e:TimerEvent):void
		{
			//direction setting
			direction=state;
			canvasInner.displayState(direction,state);
			
			//move 
			if(this.y>minY && state=="up" ){
				this.y-=1;
			}
			else if(this.y<maxY && state=="down")
			{
				this.y+=1;
			}
			else if(state=="stop"){}
			else if(state=="open")
			{}
			else if(state=="close")
			{}
			
			//judge
			
			if(destination.length){
				//trace("task!"+destination.length);
				for( var i:int=0;i<destination.length;++i)
				{
					var f:Number=getFloor();
					if(f==destination[i].getFloor())
					{
						trace("get! "+id);
						//state="open";
						canvasInner.getInnerButtons()[f-1].setBack();
						if(destination.length==1)state="stop";
						if(state=="up" || state=="stop")
						{
							canvasOuter.getOuterUpButtons()[f-1].setBack();
							trace("up cancel!");
							
						}
						 if( state=="stop"|| state=="down")
						{
							canvasOuter.getOuterDownButtons()[f-1].setBack();
							trace("down cancel!");
						}
						
						destination.splice(i,1);
						//trace("Here! destinations: "+destination.length);
					}
				}
				
			}
			
			//judge stop
			if(!destination.length)
				state="stop";
			
			if(destinationBuffer.length)
			{
				for(var j:int=0;j<destinationBuffer.length;++j)
				{
					if(addTask(destinationBuffer[j]))
					{
						destinationBuffer.splice(j,1);
					}
				}
			}
		}
		
		
		public function addTask(task:Task):Boolean
		{
				if(task.getType()=="here")
				{
					destination.push(task);
					//var dynText=stage.getChildByName("here") as TextField;
					//dynText.text="No."+id+" elevator is here!";
					return true;
				}
			
			
				//trace(getFloor());	
				if((state=="up" || state=="stop" )&& task.getFloor()>getFloor())
				{
					destination.push(task);
					trace("Yes! "+task.getFloor());
					state="up";
					return true;
				}
				else if((state=="down"|| state == "stop") && task.getFloor()<getFloor())
				{
					destination.push(task);
					trace("Yes! "+task.getFloor());
					state="down";
					return true
				}
				else if(task.getType()=="inner")
				{
					task.setType("innerWait");
					destinationBuffer.push(task);
					return true
				}
				else if(task.getType()=="innerWait")
				{
					return false;
				}
			
			return false;
		}
		
		public function getFloor():Number
		{
			//compute the floor
			//return 
			//trace(Math.floor((520-this.y)/20+0.5));
			return (520-this.y)/20.0;
			
		}
		
		public function getState():String
		{
			return state;
		}
		
		
	
	
	}
	
}